var spot = 0;
var zSpot = 100;

var camX = 20;
var camY = 20;
var camZ = 20;
var camDistance = 50;

var firstDrag = false;
var thisDragX = 0;
var thisDragY = 0;

var totalDragX = 0;
var totalDragY = 0;

var radious = 1600;

function dragHandler(e) {
	if(firstDrag) {
		firstDrag = false;
		thisDragX = e.pageX;
		thisDragY = e.pageY;
	}
	var xDelta = e.pageX - thisDragX;
	var yDelta = e.pageY - thisDragY;
	yDelta*=-1;
	if(xDelta==0 && yDelta==0) return;
	thisDragX=e.pageX;
	thisDragY=e.pageY;
	totalDragX += xDelta;
	totalDragY += yDelta;
	console.log(totalDragX + '/' + totalDragY);
	$('#dragX').text(totalDragX);
	$('#dragY').text(totalDragY);
	$('#mouseX').text(e.pageX);
	$('#mouseY').text(e.pageY);

	theta = totalDragX;
	phi = totalDragY;
	//phi = Math.min(180, Math.max(0, phi));

	camX = radious * Math.sin(theta * Math.PI / 360) * Math.cos(phi * Math.PI / 360);
	camY = radious * Math.sin(phi * Math.PI / 360);
	camZ = radious * Math.cos(theta * Math.PI / 360) * Math.cos(phi * Math.PI / 360);
	camera.updateMatrix();
}

function startDragHandler() {
	firstDrag = true;
	thisDragX = 0;
	thisDragY = 0
}

function stopDragHandler() {
	thisDragX = 0;
	thisDragY = 0;
}

function positionCamera() {
	$('#zInfo').text(camDistance);
	camera.position.x = camX;
	camera.position.y = camY;
	camera.position.z = camZ;
	camera.position.normalize();
	camera.position.multiplyScalar(camDistance);
	//	camera.position.x = Math.sin(spot);
	//	camera.position.y = Math.cos(spot);
	//	camera.position.z = zSpot;
	$('#camX').text(camera.position.x);
	$('#camY').text(camera.position.y);
	$('#camZ').text(camera.position.z);
}